package Java.ArraySort;
//而二分查找
public class Demo02 {
    public static void main(String[] args) {
        int[] array = new int[10];
        for (int i = 0; i <array.length;i++) {
            array[i] =i;}
        System.out.println("要查找的元素索引为："+halfIndex(array,6));
//        for (int index: array) {
//            System.out.println(index);
//        }

    }

    public static int halfIndex(int[] array,int value) {
        int prefix = 0;int suffix = array.length-1;int index = (prefix+suffix)/2;
        while (prefix<=suffix) {
            if (value<array[index]) {
                suffix = index-1;
                index = (prefix+suffix)/2;
            }else if (value>array[index]) {
                prefix = index+1;
                index = (prefix+suffix)/2;
            }else{
                return index;
            }
        }
        return -1;
    }
}
